<?php

namespace Home\Controller;
use Think\Controller;
use THink\Upload;
class InstallController extends Controller {
	
	public function __construct(){
		parent::__construct();
		
		if(empty($_SESSION['user']))
		{
			$this->error('对不起,您还没有登录!请先登录!', U('Login/login'), 1);
			
		}
	}
	
	
	//添加账号 没有就新增
	public function add_account(){
		if($_POST){
			$data['username']		=		empty($_REQUEST['user_name'])?'':trim($_REQUEST['user_name']);
			$data['password']		=		empty($_REQUEST['password'])?'':md5(trim($_REQUEST['password']));
			$data['cost_price']		=		empty($_REQUEST['cost_price'])?'':$_REQUEST['cost_price'];
			$data['supplier']		=		empty($_REQUEST['supplier'])?'':$_REQUEST['supplier'];
			$data['orders']			=		empty($_REQUEST['check_order'])?'':$_REQUEST['check_order'];
			$data['purchase']		=		empty($_REQUEST['purchase'])?'':$_REQUEST['purchase'];
			$data['to_purchase']	=		empty($_REQUEST['to_purchase_del'])?'':$_REQUEST['to_purchase_del'];
			$data['delivery']		=		empty($_REQUEST['delivery_del'])?'':$_REQUEST['delivery_del'];
			$data['complete']		=		empty($_REQUEST['complete_del'])?'':$_REQUEST['complete_del'];
			$data['order']			=		empty($_REQUEST['order'])?'':$_REQUEST['order'];
			$data['data']			=		empty($_REQUEST['data'])?'':$_REQUEST['data'];
			$data['system_install']	=		empty($_REQUEST['system_install'])?'':trim($_REQUEST['system_install']);
			$data['new_finish']		=		empty($_REQUEST['new_finish'])?'2':trim($_REQUEST['new_finish']);

			$data['look_details']	=		empty($_REQUEST['look_details'])?'2':trim($_REQUEST['look_details']);
			$data['del_history']	= 		empty($_REQUEST['del_history'])?0:trim($_REQUEST['del_history']);
			$data['premarks']		= 		empty($_REQUEST['remarks'])?0:trim($_REQUEST['remarks']);
			$data['select']			= 		empty($_REQUEST['select'])?0:trim($_REQUEST['select']);
			$data['recycle_product']= 		empty($_REQUEST['recycle_product'])?0:trim($_REQUEST['recycle_product']);
			$data['del_circulation']= 		empty($_REQUEST['del_circulation'])?0:trim($_REQUEST['del_circulation']);
			$data['del_offshelf_infos']= 		empty($_REQUEST['del_offshelf_infos'])?0:trim($_REQUEST['del_offshelf_infos']);
			$account = M("account");
			
			
			$where['username'] = $data['username'];
			$result = $account->where($where)->find();
			//防止重复添加用户
			if($result == false){
				$res = $account->add($data);
				$this->success("添加成功",U('account_list'),'3');exit;
			}else{
				$this->error("用户已存在",'add_account','3');exit;
			}
		}
		$this->display();	
	}
	
	//分页显示账号列表
	public function account_list(){
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		$username=empty($_REQUEST['username'])?'':$_REQUEST['username'];
		
		
		
		$account =D('account'); 
		$res = $account->account_list($pageNo,$pageSize,$where);
		$result = $res['account_list'];
		$count = $res['count'];
		
		if($_POST && !empty($username)){
			$name = array('like','%'.$username.'%');
			$result = M('account')->field('id,username')->where(array('username'=>$name))->select();
			$count['count'] = count($result);
			
			
		}
		$this->assign("account_info",$result);
		$this->assign("count",$count);
		$this->display();	
	}
	
	//账号权限管理
	public function edit_account_settings(){
		$where['id']=$_REQUEST['id'];
		$account = M("account");
		$account = $account->where($where)->find();
		
		$cost 			= 	$account['cost_price'];
		$supplier 		= 	$account['supplier'];
		$orders 		= 	$account['orders'];
		$purchase 		= 	$account['purchase'];
		$to_purchase	= 	$account['to_purchase'];
		$delivery		=	$account['delivery'];
		$complete 		= 	$account['complete'];
		$order			=	$account['order'];
		$data			=	$account['data'];
		$system_install = 	$account['system_install'];
		$new_finish		=	$account['new_finish'];
		$look_details	=	$account['look_details'];
		$del_circulation	=	$account['del_circulation'];
		$del_offshelf_infos	=	$account['del_offshelf_infos'];
		$recycle_product 	=   $account['recycle_product'];


		$this->assign('del_history',$account['del_history']);
		$this->assign('premarks',$account['premarks']);
		$this->assign('select',$account['select']);
		$this->assign("account",$account);
		$this->assign("supplier",$supplier);
		$this->assign("orders",$orders);
		$this->assign("purchase",$purchase);
		$this->assign("to_purchase",$to_purchase);
		$this->assign("delivery",$delivery);
		$this->assign("complete",$complete);
		$this->assign("new_finish",$new_finish);
		$this->assign("look_details",$look_details);
		$this->assign("order",$order);
		$this->assign("data",$data);
		$this->assign("system_install",$system_install);
		$this->assign("cost",$cost);
		$this->assign("del_circulation",$del_circulation);
		$this->assign("del_offshelf_infos",$del_offshelf_infos);
		$this->assign("recycle_product",$recycle_product);
		$this->display();	
	}
	
	//账号权限修改
	public function edit_account(){
		$where['id']				=		$_POST['id'];
		$data['username']			=		trim($_POST['username']);
		$password			=		trim($_POST['password']);
		$password = trim($_POST['password']);
		$md5 = M('account')->field('password')->where(array('username'=>$data['username']))->find();
		if($md5['password'] != $password){
			$data['password']			=		md5(trim($_POST['password']));
		}else{
			$data['password'] = $md5['password'];
		}
		$data['cost_price']			=		$_POST['cost_price'];
		$data['supplier']			=		$_POST['supplier'];
		$data['orders']				=		$_POST['orders'];
		$data['purchase']			=		$_POST['purchase'];
		$data['to_purchase']		=		$_POST['to_purchase_del'];
		$data['delivery']			=		$_POST['delivery_del'];
		$data['complete']			=		$_POST['complete_del'];
		$data['order']				=		$_POST['order'];
		$data['data']				=		$_POST['data'];
		$data['system_install']		=		$_POST['system_install'];
		$data['new_finish']			=		$_POST['new_finish'];
		$data['look_details']		=		$_POST['look_details'];
		$data['del_history']		= 		empty($_REQUEST['del_history'])?0:trim($_REQUEST['del_history']);
		$data['premarks']			= 		empty($_REQUEST['remarks'])?0:trim($_REQUEST['remarks']);
		$data['select']				= 		empty($_REQUEST['select'])?0:trim($_REQUEST['select']);
		$data['del_circulation']			=		$_POST['del_circulation'];
		$data['del_offshelf_infos']			=		$_POST['del_offshelf_infos'];
		$data['recycle_product']			=		$_POST['recycle_product'];


		if(empty($data['cost_price'])){
			$data['cost_price']='2';	
		}
		if(empty($data['supplier'])){
			$data['supplier']='2';	
		}
		if(empty($data['order'])){
			$data['order']='2';	
		}
		if(empty($data['data'])){
			$data['data']='2';	
		}
		if(empty($data['to_purchase'])){
			$data['to_purchase']='2';	
		}
		if(empty($data['delivery'])){
			$data['delivery']='2';	
		}
		if(empty($data['complete'])){
			$data['complete']='2';	
		}
		if(empty($data['new_finish'])){
			$data['new_finish']='2';	
		}
		if(empty($data['look_details'])){
			$data['look_details']='2';	
		}
		if(empty($data['del_circulation'])){
			$data['del_circulation']='0';	
		}
		if(empty($data['del_offshelf_infos'])){
			$data['del_offshelf_infos']='0';	
		}

		//dump($data);exit;
		$account = M("account");
		$result = $account->where($where)->save($data);
		
		if($result){
			$this->success("修改成功",U('account_list'),"1");
		}else{
			$this->error("您没有权限修改配置",U('account_list'),"1");
		}
		
		
	}
	
	//删除账号
	public function del_account(){
		$where['id']=empty($_REQUEST['id'])?0:$_REQUEST['id'];
		
		$account = M("account");
		$result = $account->where($where)->delete();
		if($result){
			
			header("Location:".$_SERVER['HTTP_REFERER']);
		}
	}
	
	

	
	//团队列表
	public function salesman_team(){
		$team = M("team");
		$team_list = $team->group('team_name')->select();
		foreach($team_list as $key=>$val){
			$team = M("team")->where(array('team_name'=>$val['team_name']))->select();
			$arr = explode("|",$val['team_members']);
			$team_list[$key]['team_members'] = implode('<br/>',$arr);
		}
		$this->assign("team_list",$team_list);
		$this->display();	
	}
	
	
	
	
	//自定义管理
	public function custom(){
		$where['id']			=	empty($_REQUEST['id'])?1:trim($_REQUEST['id']);
		$custom = M("custom");
		$result = $custom->find();

		$where2['id']			=	empty($_REQUEST['id'])?1:trim($_REQUEST['id']);
		$platform = M("platform");
		$result2 = $platform->find();

		if($_POST){
			
			$data['one']			=	empty($_REQUEST['one'])?'':trim($_REQUEST['one']);
			$data['two']			=	empty($_REQUEST['two'])?'':trim($_REQUEST['two']);
			$data['three']			=	empty($_REQUEST['three'])?'':trim($_REQUEST['three']);
			$data['four']			=	empty($_REQUEST['four'])?'':trim($_REQUEST['four']);
			
			if($result){
				$res = $custom->where($where)->save($data);
			}else{
				$res = $custom->add($data);
			}
		}
		$this->assign("result",$result);
		$this->assign("result2",$result2);
		$this->display();
	}


	
	
	//汇率设置
	public function exchange_rate(){
		$where['id']			=	empty($_REQUEST['id'])?1:trim($_REQUEST['id']);
		$result = M("exchange_rate")->where($where)->find();
		if($_POST){
			
			$data['USD']			=	empty($_REQUEST['USD'])?'':trim($_REQUEST['USD']);
			$data['GBP']			=	empty($_REQUEST['GBP'])?'':trim($_REQUEST['GBP']);
			$data['JPY']			=	empty($_REQUEST['JPY'])?'':trim($_REQUEST['JPY']);
			$data['EUR']			=	empty($_REQUEST['EUR'])?'':trim($_REQUEST['EUR']);
			$data['AUD']			=	empty($_REQUEST['AUD'])?'':trim($_REQUEST['AUD']);
			$data['CAD']			=	empty($_REQUEST['CAD'])?'':trim($_REQUEST['CAD']);
			$data['RUB']			=	empty($_REQUEST['RUB'])?'':trim($_REQUEST['RUB']);
			$data['MYR']			=	empty($_REQUEST['MYR'])?'':trim($_REQUEST['MYR']);
			$data['SGD']			=	empty($_REQUEST['SGD'])?'':trim($_REQUEST['SGD']);
			$data['PHP']			=	empty($_REQUEST['PHP'])?'':trim($_REQUEST['PHP']);
			$data['THB']			=	empty($_REQUEST['THB'])?'':trim($_REQUEST['THB']);
			$data['IDR']			=	empty($_REQUEST['IDR'])?'':trim($_REQUEST['IDR']);
			$data['MXN']			=	empty($_REQUEST['MXN'])?'':trim($_REQUEST['MXN']);
			
			if($result){
				$res = M("exchange_rate")->where($where)->save($data);
				$this->success("修改成功","exchange_rate",'3');exit;
			}else{
				$res = M("exchange_rate")->add($data);
				$this->success("添加成功","exchange_rate",'3');exit;
			}
		}
		$this->assign("result",$result);
		$this->display();
	}
	
	
	//收款方式设置
	public function payment(){
		if(I("post.payment_method","","htmlspecialchars")){
			$payment_method = I('post.payment_method','','htmlspecialchars');
			$verify = M("payment")->where(array('payment_method'=>$payment_method))->find();
			if(empty($verify)){
				$payment_list = M("payment")->add(array('payment_method'=>$payment_method));
				$this->success("添加成功","payment",'3');exit;
			}else{
				$this->success("添加失败，已存在","payment",'3');exit;
			}
		}
		
		
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		
		$install =D('Install'); 
		$res = $install->payment_list($pageNo,$pageSize);
		
		$payment_list = $res['payment_list'];
		$count = $res['count'];
		
		$this->assign("count",$count);
		$this->assign('payment_list',$payment_list);
		$this->display();
	}
	
	//删除支付方式
	public function payment_dele(){
		$id = I('get.p_id','','htmlspecialchars');
		$payment_list = M("payment")->where(array('id'=>$id))->delete();
		//$this->success("删除成功","payment",'3');exit();
		header('Location:'.$_SERVER['HTTP_REFERER']);  
	}
	
	

	//网页设置
	public function web_settings(){
		$webcnf = M('web_config');  //实例化
		if(I('post.')){  //接收新增

			$data = I('post.');
			//dump($data);

			//dump($_FILES);die;
            if( isset($_FILES['logo_img']['tmp_name']) && !empty($_FILES['logo_img']['tmp_name']) ){
               $path = '/Public/Uploads/';
               $getPicRoute = $this->file_upload($_FILES['logo_img'],$path);
               //dump($getPicRoute);die;
               if($getPicRoute){
                   $data['logo_img'] = $getPicRoute;
               }
            }

            //banner1
            if( isset($_FILES['banner1']['tmp_name']) && !empty($_FILES['banner1']['tmp_name']) ){
               $path = '/Public/banners/';
               $getPicRoute = $this->file_upload($_FILES['banner1'],$path);
               //dump($getPicRoute);die;
               if($getPicRoute){
                   $data['banner1'] = $getPicRoute;
               }
            }

            //banner2
            if( isset($_FILES['banner2']['tmp_name']) && !empty($_FILES['banner2']['tmp_name']) ){
               $path = '/Public/banners/';
               $getPicRoute = $this->file_upload($_FILES['banner2'],$path);
               //dump($getPicRoute);die;
               if($getPicRoute){
                   $data['banner2'] = $getPicRoute;
               }
            }


            //banner3
            if( isset($_FILES['banner3']['tmp_name']) && !empty($_FILES['banner3']['tmp_name']) ){
               $path = '/Public/banners/';
               $getPicRoute = $this->file_upload($_FILES['banner3'],$path);
               //dump($getPicRoute);die;
               if($getPicRoute){
                   $data['banner3'] = $getPicRoute;
               }
            }

            //banner4
            if( isset($_FILES['banner4']['tmp_name']) && !empty($_FILES['banner4']['tmp_name']) ){
               $path = '/Public/banners/';
               $getPicRoute = $this->file_upload($_FILES['banner4'],$path);
               //dump($getPicRoute);die;
               if($getPicRoute){
                   $data['banner4'] = $getPicRoute;
               }
            }



            //dump($data);die;

			if($data['id']){
				$res = $webcnf->save($data);
				if($res!==false){
					$this->success('成功更新网页配置','web_settings');
				}else{
					$this->error('服务器忙碌，请稍后再试!');
				}
			}else{
				$res = $webcnf->add($data);
				if($res){
					$this->success('成功设置网页配置','web_settings');
				}else{
					$this->error('服务器忙碌，请稍后再试!');
				}

			}
			

		}

		

		//显示
		$getConf = $webcnf->find(1);
		$getConf['web_logo'] = '/Public/Uploads/'.$getConf['web_logo'];
		$this->assign('getConf',$getConf);  //赋值模板
		$this->display();
	}

	//文件上传公用方法
	public function file_upload($filename,$path){


        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');//设置附件上传类型
        $upload->rootPath  =      ROOT_PATH.$path; // 设置附件上传根目录
        $upload->savePath  =      ''; // 设置附件上传（子）目录
// 上传文件
        //判断目录是否存在

            $info   =   $upload->uploadOne($filename);
            if(!$info) {// 上传错误提示错误信息
                //$this->error($upload->getError());
                return false;
            }else{// 上传成功 获取上传文件信息
//                foreach($info as $file){
//                    echo $file['savepath'].$file['savename'];
//                }
                return $info['savepath'].$info['savename'];
            }



    }

		
	
 	
}
















